﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TransCode.Entitys;
using TransCode.Logic;

namespace TransCode.Player.ImagePage
{
    /// <summary>
    /// Interaction logic for ImageBoxItem.xaml
    /// </summary>
    public partial class ImageBoxItem : UserControl
    {
        static DependencyProperty TargetObjectProperty;

        public ImageBoxItem()
        {
            InitializeComponent();
        }

        static ImageBoxItem()
        {
            TargetObjectProperty = DependencyProperty.Register(
                "TargetObject", typeof(TargetObject), typeof(ImageBoxItem),
                new PropertyMetadata(null,
                (DependencyObject d, DependencyPropertyChangedEventArgs e) =>
                {
                    ImageBoxItem ui = d as ImageBoxItem;
                    TargetObject to = e.NewValue as TargetObject;

                    BitmapImage source = new BitmapImage();
                    source.BeginInit();
                    source.UriSource = new Uri(to.ImagePath + to.ImageName, UriKind.RelativeOrAbsolute);
                    source.EndInit();
                    ui.img.Source = source;
                })
            );
        }

        public TargetObject TargetObject
        {
            get { return GetValue(ImageBoxItem.TargetObjectProperty) as TargetObject; }
            set { SetValue(ImageBoxItem.TargetObjectProperty, value); }
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            queryImg.Visibility = System.Windows.Visibility.Visible;
            playImg.Visibility = System.Windows.Visibility.Visible;
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            queryImg.Visibility = System.Windows.Visibility.Hidden;
            playImg.Visibility = System.Windows.Visibility.Hidden;
        }

        public event EventHandler<TargetObjectFindResultArgs> ImageQueryEvent;
        public event EventHandler<TargetObjectFindResultArgs> PlayerEvent;

        private void queryImg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ImageQueryEvent != null)
                ImageQueryEvent(this, new TargetObjectFindResultArgs() { Result = TargetObject });
        }

        private void playImg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (PlayerEvent != null)
                PlayerEvent(this, new TargetObjectFindResultArgs() { Result = TargetObject });

            VideoLogic logic = new VideoLogic();
            Video video = logic.Single(TargetObject.VideoId);
            Common.VideoMainPage.Play(video);
            Common.Change(FuncEnum.VideoPlay);
        }
    }
}
